import requests
import time

class HttpClient:
    def __init__(self):
        self.session = requests.session()
       #**kwargs:可以同时接收多个参数

        #get请求方法
    def get_request(self,url,data,**kwargs):
        response = self.session.get(url=url,params=data,**kwargs)
        result = response.text
        result_time = response.elapsed.total_seconds()
        return result,result_time
        #post请求方法
    def post_request(self,url,data,**kwargs):
        if type(data)is dict:
            response = self.session.post(url=url,data=data,**kwargs)
            result = (response.text).encode('utf-8').decode('unicode-escape')
            result_time = response.elapsed.total_seconds()
            print("接口响应报文：%s"%result+"\n","接口响应时间：%s"%result_time)
            return result,result_time
        else:
            response = self.session.post(url=url, json=data, **kwargs)
            result = (response.text).encode('utf-8').decode('unicode-escape')
            result_time = response.elapsed.total_seconds()
            print("接口响应报文：%s" % result + "\n", "接口响应时间：%s" % result_time)
            return result, result_time

        #delete请求方法
    def delete_request(self,url,data,**kwargs):
        if type(data) is dict:
            response = self.session.post(url=url, data=data, **kwargs)
            result = (response.text).encode('utf-8').decode('unicode-escape')
            result_time = response.elapsed.total_seconds()
            print("接口响应报文：%s" % result + "\n", "接口响应时间：%s" % result_time)
            return result, result_time
        else:
            response = self.session.post(url=url, json=data, **kwargs)
            result = (response.text).encode('utf-8').decode('unicode-escape')
            result_time = response.elapsed.total_seconds()
            print("接口响应报文：%s" % result + "\n", "接口响应时间：%s" % result_time)
            return result, result_time
        #put请求方法
    def put_request(self,url,data,**kwargs):
        if type(data) is dict:
            response = self.session.post(url=url, data=data, **kwargs)
            result = (response.text).encode('utf-8').decode('unicode-escape')
            result_time = response.elapsed.total_seconds()
            print("接口响应报文：%s" % result + "\n", "接口响应时间：%s" % result_time)
            return result, result_time
        else:
            response = self.session.post(url=url, json=data, **kwargs)
            result = (response.text).encode('utf-8').decode('unicode-escape')
            result_time = response.elapsed.total_seconds()
            print("接口响应报文：%s" % result + "\n", "接口响应时间：%s" % result_time)
            return result, result_time

        #关闭会话
    def close_session(self):
        self.session.close()
